package com.irdstudio.tdp.executor.core.plugin.dataload;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 数据表操作类-[表名: 03.数据装载结果表(plugin_load_result)]
 * 通过JDBC实现基本的数据表操作(CRUD) 
 * @author 代码自动生成
 * @version 1.0
 * @date 2014-05-26
 */
public class PluginLoadResultDao {

	private static final Logger logger = LoggerFactory.getLogger(PluginLoadResultDao.class);
	/* 连接对象 */
	Connection conn = null;	

	public PluginLoadResultDao(Connection conn){
		this.conn = conn;
	}

	/**
	 * 根据主键查询单条记录
	 * @param 
	 * @return
	 * @throws SQLException
	 */
	public PluginLoadResult queryWithKeys(String batchSerialNo,String tableName)
			throws SQLException {
		PluginLoadResult dc = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			ps = conn
					.prepareStatement("SELECT * FROM plugin_load_result WHERE batch_serial_no=?,table_name=?");
			ps.setString(1,batchSerialNo);
			ps.setString(2,tableName);
			rs = ps.executeQuery();
			if (rs.next()) {
				dc = new PluginLoadResult();
				dc.setBatchSerialNo(rs.getString("batch_serial_no"));
				dc.setBatchDate(rs.getString("batch_date"));
				dc.setBatchId(rs.getString("batch_id"));
				dc.setUpSysname(rs.getString("up_sysname"));
				dc.setTableName(rs.getString("table_name"));
				dc.setTableCnname(rs.getString("table_cnname"));
				dc.setTableType(rs.getString("table_type"));
				dc.setTableLoadMode(rs.getString("table_load_mode"));
				dc.setLoadFromFile(rs.getString("load_from_file"));
				dc.setFileSize(rs.getBigDecimal("file_size"));
				dc.setStartTime(rs.getString("start_time"));
				dc.setCostTime(rs.getBigDecimal("cost_time"));
				dc.setEndTime(rs.getString("end_time"));
				dc.setReadRows(rs.getInt("read_rows"));
				dc.setLoadRows(rs.getInt("load_rows"));
				dc.setRejectRows(rs.getInt("reject_rows"));
				dc.setLoadResult(rs.getString("load_result"));
				dc.setRemark(rs.getString("remark"));
			}
		} catch (SQLException e) {
			throw new SQLException("queryPluginLoadResultWithKeys is Wrong!"
					+ e.getMessage());
		} finally {
			close(rs, null, ps);
		}
		return dc;
	}

	/**
	 * 根据查询条件查询多条或单条记录
	 * @param szCondition 
	 * @return
	 * @throws SQLException
	 */
//	public List<PluginLoadResult> queryWithCond(String szCondition,String szOrderBy)
//			throws SQLException {
//		List<PluginLoadResult> dcList = new ArrayList<PluginLoadResult>();
//		PreparedStatement ps = null;
//		ResultSet rs = null;
//		try {
//			ps = conn.prepareStatement("SELECT * FROM plugin_load_result "
//					+ szCondition + " " + szOrderBy);
//			rs = ps.executeQuery();
//			while (rs.next()) {
//				PluginLoadResult dc = new PluginLoadResult();
//				dc.setBatchSerialNo(rs.getString("batch_serial_no"));
//				dc.setBatchDate(rs.getString("batch_date"));
//				dc.setBatchId(rs.getString("batch_id"));
//				dc.setUpSysname(rs.getString("up_sysname"));
//				dc.setTableName(rs.getString("table_name"));
//				dc.setTableCnname(rs.getString("table_cnname"));
//				dc.setTableType(rs.getString("table_type"));
//				dc.setTableLoadMode(rs.getString("table_load_mode"));
//				dc.setLoadFromFile(rs.getString("load_from_file"));
//				dc.setFileSize(rs.getBigDecimal("file_size"));
//				dc.setStartTime(rs.getString("start_time"));
//				dc.setCostTime(rs.getBigDecimal("cost_time"));
//				dc.setEndTime(rs.getString("end_time"));
//				dc.setReadRows(rs.getInt("read_rows"));
//				dc.setLoadRows(rs.getInt("load_rows"));
//				dc.setRejectRows(rs.getInt("reject_rows"));
//				dc.setLoadResult(rs.getString("load_result"));
//				dc.setRemark(rs.getString("remark"));
//				dcList.add(dc);
//			}
//		} catch (SQLException e) {
//			throw new SQLException("queryPluginLoadResultWithCond is Wrong!"
//					+ e.getMessage());
//		} finally {
//			close(rs, null, ps);
//		}
//		return dcList;
//	}

	/**
	 * 关闭资源
	 * @param theRs
	 * @param theStmt
	 * @param thePs
	 */
	protected void close(ResultSet theRs, Statement theStmt,
			PreparedStatement thePs) {
		try {
			if (theRs != null)
				theRs.close();
			if (theStmt != null)
				theStmt.close();
			if (thePs != null)
				thePs.close();
		} catch (SQLException e) {
			logger.error(e.getMessage());
		}
	}

}
